<?php

# Setup classes
include_once(dirname(__FILE__).'/ajaxResource.php');

# Define variables
$invalidInput = false;
$time = time();

# Resource rating
if($siteBuilder->post['type'] == 'resource') {
	
	$key = 'resourceID';
	$table = 'resources_comments';	
	
} elseif($siteBuilder->post['type'] == 'book') {
	
	$key = 'ISBN';
	$table = 'books_comments';	
	
} else {

	$invalidInput = true;	
	
}

# We did something wrong
if($invalidInput !== true) {
	
	# No error
	$return['error'] = false;
	
	# Clean text
	$siteBuilder->post['value'] = nl2br(htmlentities(strip_tags($_POST['value']), ENT_QUOTES, 'UTF-8'));
				
	# Insert comment
	$siteBuilder->db->query("INSERT INTO comments SET userID = '{$siteBuilder->user['userID']}', comment = '{$siteBuilder->post['value']}', status = 1, time = '{$time}'");
	$commentID = $siteBuilder->db->insertID;
		
	# Insert rating
	$siteBuilder->db->query("INSERT INTO {$table} SET {$key} = '{$siteBuilder->post['ID']}', commentID = '{$commentID}'");	
		
	# Alter rating
	$return['comment'] = '<article>
		<h5>
			<aside>'.$siteBuilder->timeSince($time - 1).'</aside>
			'.$siteBuilder->user['firstName'].' '.$siteBuilder->user['surname'].'
		</h5>
		<p>'.html_entity_decode($siteBuilder->post['value'], ENT_QUOTES, 'UTF-8').'</p>
	</article>';
	
} else {
	
	$return['error'] = true;
	
}

# Return response
echo(json_encode($return));

# Release memory
unset($siteBuilder);

?>